package com.itheima.crops.controller;

import com.github.pagehelper.Page;
import com.itheima.crops.pojo.Disease;
import com.itheima.crops.service.DiseaseService;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/disease")
public class DiseaseController {
    @Autowired
    private DiseaseService diseaseService;

    /**
     * 农作物病虫害全查
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<Disease> diseaseList = diseaseService.findAll();
            return new Result(true, StatusCode.OK,"查询农作物病虫害成功！",diseaseList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询农作物病虫害失败！");
        }
    }

    /**
     * 农作物病虫害根据id查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            Disease disease = diseaseService.findById(id);
            return new Result(true,StatusCode.OK,"根据id查询农作物病虫害成功！",disease);
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物病虫害不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"根据id查询农作物病虫害失败，该农作物病虫害不存在！");
            }
            return new Result(false,StatusCode.ERROR,"根据id查询农作物病虫害失败!");
        }
    }

    /**
     * 新增农作物病虫害
     * @param disease
     */
    @PostMapping("/add")
    public Result add(@RequestBody Disease disease) {
        try {
            diseaseService.add(disease);
            return new Result(true,StatusCode.OK,"新增农作物病虫害成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.OK,"新增农作物病虫害失败！");
        }
    }

    /**
     * 修改农作物病虫害
     * @param disease
     */
    @PostMapping("/update")
    public Result update(@RequestBody Disease disease) {
        try {
            diseaseService.update(disease);
            return new Result(true,StatusCode.OK,"修改农作物病虫害成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物病虫害不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"修改农作物病虫害失败，该农作物病虫害不存在！");
            }
            return new Result(true,StatusCode.ERROR,"修改农作物病虫害失败！");
        }
    }

    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable(name = "page")  int page, @PathVariable(name = "size")  int size){
        try {
            Page<Disease> pageList = diseaseService.findPage(searchMap, page, size);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,"查询成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.ERROR,"查询成功");
        }
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        try {
            diseaseService.delete(id);
            return new Result(true,StatusCode.OK,"删除农作物病虫害成功！");
        } catch (Exception e) {
            e.printStackTrace();
            if("农作物病虫害不存在".equals(e.getMessage())) {
                return new Result(false,StatusCode.NOTEXIST,"删除农作物病虫害失败，该农作物病虫害不存在！");
            }
            if("该病虫害与农作物关联".equals(e.getMessage())) {
                return new Result(false,StatusCode.EXISTREL,"删除农作物病虫害失败，该病虫害与农作物关联！");
            }
            return new Result(false,StatusCode.ERROR,"删除农作物病虫害失败！");
        }
    }
}
